package com.g4.shoppingback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.mapper.DdLogsMapper;
import com.g4.shoppingback.pojo.DdLogs;
import com.g4.shoppingback.pojo.DdUsersInfo;
import com.g4.shoppingback.pojo.Role;
import com.g4.shoppingback.service.DdLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DdLogsServiceImpl implements DdLogsService {

    @Autowired
    private DdLogsMapper ddLogsMapper;

    /**
     * 添加日志
     * @param ddLogs 日志数据
     */
    @Override
    public void addLog(DdLogs ddLogs) {
        ddLogsMapper.insert(ddLogs);
    }

    /**
     * 根据分页信息或用户名查询日志
     * @param pageVo 封装了查询关键字和页数
     * @return 返回分页信息
     */
    @Override
    public PageVo selectLogsByPaged(PageVo pageVo) {
        Integer pageSize = 5;   //设置每页固定显示5条数据
        QueryWrapper<DdLogs> queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.hasLength(pageVo.getQuery()),"username", pageVo.getQuery());
        Long total = Long.valueOf(ddLogsMapper.selectCount(queryWrapper)); //查询总条数
        Page<DdLogs> page =     //根据分页信息和条件构造器查询
                ddLogsMapper.selectPage(new Page(pageVo.getPageNum(),pageSize), queryWrapper);
        List<DdLogs> list = page.getRecords();  //通过getRecords()获取查询结果
        pageVo.setPageSize(pageSize).setTotal(total).setRows(list); //封装分页信息
        return pageVo;
    }
}
